package com.kolon.business.mngr.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.mngr.service.dao.ServiceDao;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.PageUtil;

@Controller("com.kolon.business.mngr.service.controller.ServiceListController")
public class ServiceListController implements SimpleController {

	@Resource(name="com.kolon.business.mngr.service.dao.ServiceDaoImpl")
	private ServiceDao serviceDao;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/service/serviceList.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		// 파라미터
		int page = ComUtil.sToi(ComUtil.nullChk(request.getParameter("page"), "1"));
		String schTp = ComUtil.nullChk(request.getParameter("schTp"));
		String schTxt = ComUtil.nullChk(request.getParameter("schTxt"));
		map.put("schTp", schTp);
		map.put("schTxt", schTxt);
		int recordPerPage = 20;
		// 서비스 총 수
		int totCnt = serviceDao.getCount(map);
		// 페이지 네비게이터 생성
		PageUtil pageUtil = new PageUtil(
				page
			   ,request.getContextPath()
			   ,"/business/mngr/service/serviceList.do"
			   ,10
			   ,recordPerPage
			   ,totCnt
			   ,"&amp;schTp=" + schTp + 
			    "&amp;schTxt=" + ComUtil.getEncode(schTxt, "UTF-8"));
		int startIndex = pageUtil.getRecordPerPage() * (page - 1);
		// 목록 조회
		List list = serviceDao.getPageList(map, startIndex, pageUtil.getRecordPerPage());
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("totCnt", totCnt);
		modelAndView.addObject("list", list);
		modelAndView.addObject("page", page);
		modelAndView.addObject("schTp", schTp);
		modelAndView.addObject("schTxt", schTxt);
		modelAndView.addObject("schTxtUTF8", ComUtil.getEncode(schTxt, "UTF-8"));
		modelAndView.addObject("pageHtml", pageUtil.getMakePage());
		modelAndView.addObject("pageCnt", pageUtil.getTotalNumOfPage());
		modelAndView.addObject("recordPerPage", recordPerPage);
		return modelAndView;
	}
}
